package org.ftc.web;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.ftc.domain.RoleAuth;
import org.ftc.form.RoleAuthForm;
import org.ftc.service.FtcLogService;
import org.ftc.service.FtcLogService.Log;
import org.ftc.service.RoleAuthService;
import org.ftc.service.UsersService;
import org.ftc.util.ListCalculator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller 
public class RoleAuthController {
	
	@Autowired
	private RoleAuthService roleAuthService;
	@Autowired
	private UsersService usersService;
	@Autowired
	private FtcLogService logService;
	
	@RequestMapping(value = "/roleAuth.htm")
    public String roleAuth( ModelMap model ) {
		model.addAttribute("searchForm", new RoleAuthForm() );
		return "roleAuth/list";
    }
	
	@RequestMapping(value = "/roleAuth/list.json" , method=RequestMethod.POST)
	public @ResponseBody Map<String, Object> list(RoleAuthForm form , BindingResult binding, ModelMap model ) {
		logService.before();
		Map<String, Object> json = new HashMap<String, Object>();
		ListCalculator listCal = new ListCalculator(); 
		int pageSize = form.getRows();
		listCal.setRecordsPerPage( pageSize );
		Integer totalRecs = (int)roleAuthService.count(form);
		int totalPages = listCal.totalPages(totalRecs);   
		int pageNo = form.getPage();
		pageNo = (pageNo >= totalPages) ? totalPages : pageNo;
		int firstRes = listCal.firstResult(pageNo);  
		json.put("total", totalRecs.toString() ); 
		json.put("rows", roleAuthService.findPage(form, firstRes, pageSize));
		logService.save(Log.QUERY, this.getClass().toString() );
		return json; 
    }
	
	@RequestMapping(value = "/roleAuth/newForm.htm")
    public String form(	ModelMap model ) {
		model.addAttribute("roleAuthForm", new RoleAuth() );
        return "roleAuth/save";
    } 
	
	@RequestMapping(value = "/roleAuth/get.htm")
    public String get(@RequestParam("id") String id, ModelMap model ) {
		logService.before();
		model.addAttribute("roleAuthForm", roleAuthService.get(id) );
		logService.save(Log.QUERY, this.getClass().toString() );
        return "roleAuth/update";
    }
	
	@RequestMapping(value = "/roleAuth/save.htm", method=RequestMethod.POST)
    public String save(RoleAuth entity, ModelMap model ) {
		logService.before();
		roleAuthService.save(entity);
		model.addAttribute("id", entity.getRoleSeq());
		logService.save(Log.INSERT, entity.getRoleSeq(), this.getClass().toString() ,ToStringBuilder.reflectionToString(entity));
		return "redirect:/roleAuth/get.htm";  
    }
	
	@RequestMapping(value = "/roleAuth/update.htm", method=RequestMethod.POST)
    public String update(RoleAuth entity , ModelMap model ) {
		logService.before();
		RoleAuth notice = roleAuthService.get(entity.getRoleSeq());
		entity.setVersion(notice.getVersion());
		entity.setCreationDate(notice.getCreationDate());
		entity.setCreationUser(notice.getCreationUser());
		roleAuthService.update(entity);
		model.addAttribute("id", entity.getRoleSeq() );
		logService.save(Log.UPDATE, entity.getRoleSeq(), this.getClass().toString() ,ToStringBuilder.reflectionToString(entity));
		return "redirect:/roleAuth/get.htm";  
    }
	
	@RequestMapping(value = "/roleAuth/delete.json", method=RequestMethod.POST)
    public @ResponseBody boolean delete(@RequestParam("id") String id, ModelMap model ) {
		logService.before();
		logService.save(Log.DELETE, id, this.getClass().toString() );
		return roleAuthService.delete(id);
    }
	
	@RequestMapping(value = "/roleAuth/getName.json" , method=RequestMethod.GET)
    public @ResponseBody String getName(@RequestParam("id") String id) {
		logService.before();
		RoleAuth roleAuth = roleAuthService.get(id);
		logService.save(Log.QUERY, this.getClass().toString() );
		return roleAuth== null?"":roleAuth.getAuthName();
    }
	
}
